{template 'common/header'}
<style type="text/css">
	.favorite_no_nav {height:38px; width:43%; background:#eee; margin:0px 3%; float:left; border:1px solid #d4d4d4; border-radius:5px; text-align:center; line-height:38px; color:#666;}
	.favorite_no {height:100px; width:100%; margin:50px 0px 60px; color:#ccc; font-size:12px; text-align:center;}
	.favorite_no_menu {height:40px; width:100%;}
	.favorite_no_nav {height:38px; width:43%; background:#eee; margin:0px 3%; float:left; border:1px solid #d4d4d4; border-radius:5px; text-align:center; line-height:38px; color:#666;}
	#favorite_loading { padding:10px;color:#666;text-align: center;}
</style>
<div id='container'></div>
<script id='favorite_empty' type='text/html'>
	<div class="favorite_no"><i class="fa fa-favorite" style="font-size:100px;"></i><br><span style="line-height:18px; font-size:16px;">您没有浏览过任何商品</span><br>主人快去给我找点东西吧</div>
	<div class="favorite_no_menu">
		<div class="favorite_no_nav" onclick="location.href='{php echo $this->createMobileUrl('member')}'">个人中心</div>
		<div class="favorite_no_nav"  onclick="location.href='{php echo $this->footer['first']['url']}'">去逛逛</div>
	</div>
</script>
<script id="favorite_main" type="text/html">
	<div class="mui-content content-bg-w7">
		<header id="header" class="mui-bar mui-bar-nav">
			<button class="mui-action-back mui-btn mui-btn-blue mui-btn-link mui-btn-nav mui-pull-left header-back-icon">
				<span class="mui-icon mui-icon-left-nav"></span>
				我的收藏
			</button>
			<button class="mui-btn mui-btn-blue mui-btn-link mui-pull-right btn-collect-edit"  id="removefavorite">删除</button>
		</header>
		<div class="list-foot-print favorite_main"></div>
	</div>
</script>
<script id='favorite_list' type='text/html'>
	<div class="list-order">
		<ul class="list-group-order">
			<%each list as value%>
			<li class="list-item-order list-item-foot-print favorite_good" data-favoriteid="<%value.id%>" sel='0'>
				<div class="mid-content-order white-bg">
					<div class="pic-goods-order">
						<img src="<%value.thumb%>" alt="" />
					</div>
					<div class="info-goods-order">
						<div class="goods-name-order"><%value.title%></div>
						<div class="goods-intro-order">￥<%value.marketprice%> <span style="color:#666;font-size:12px;text-decoration:line-through;">￥<%value.productprice%><span></div>
					</div>
					<div class="info-goods-num-order box-collect-edit mui-checkbox">
						<input name="checkbox1" value="Item 1" type="checkbox" >
					</div>
				</div>
			</li>
			<%/each%>
		</ul>
	</div>
</script>
<script language='javascript'>

	var page= 1;
	function setSelect(obj, sel){
		sel =sel==1?0:1;
		obj.parent().parent().parent().attr('sel',sel);
		calctotal();
	}
	function calctotal(){
		var total = 0;
		$(".favorite_good").each(function(){
			var $this = $(this);
			var sel = $this.attr('sel')=='1';
			if(sel){
				total++;
			}
		});
		if(total<=0){
			$("#removefavorite").addClass('disabled');
		} else{
			$("#removefavorite").removeClass('disabled');
		}
		return total;
	}
	require(['tpl', 'core'], function(tpl, core) {
		function bindEvents(){
			mui('.mui-checkbox').on('change', 'input', function() {
				setSelect($(this),$(this).parent().parent().parent().attr('sel'));
			});
		}
		function removeFavorite(ids){
			if(ids.length<=0){
				mui.toast('未选择商品');
				return;
			}
			var btnArray = ['是', '否'];
			mui.confirm('确认删除这些收藏？', '温馨提示', btnArray, function(e){
				if(e.index == 0) {
					$('.favorite_good').attr('del', 0);
					core.json('shop/favorite', {'op': 'remove', ids: ids}, function (json) {
						if (json.status == 1) {
							for (var i in ids) {
								$('.favorite_good[data-favoriteid=' + ids[i] + ']').attr('del', 1).fadeOut(500, function () {
									$('.favorite_good[data-favoriteid=' + ids[i] + ']').remove();
								})
							}
							if ($('.favorite_good[del=0]').length <= 0) {
								$('#container').html(tpl('favorite_empty'));
							} else {
								calctotal();
							}
						} else {
							mui.toast('删除失败');
						}
					}, true, true);
				}
			});
		}
		core.json('shop/favorite',{},function(json){
			if(json.result.total<=0){
				$('#container').html(  tpl('favorite_empty') );
				return;
			}

			$('#container').html(  tpl('favorite_main',json.result) );

			$('#removefavorite').click(function(){
				var ids = [];
				$('.favorite_good[sel=1]').each(function(){
					ids.push($(this).data('favoriteid')) ;
				});
				removeFavorite(ids);
			});


			$('.favorite_main').html(  tpl('favorite_list',json.result) );
			$('.box-collect-edit').show();
			bindEvents();

			var loaded = false;
			var stop=true;
			$(window).scroll(function(){
				if(loaded){
					return;
				}
				totalheight = parseFloat($(window).height()) + parseFloat($(window).scrollTop());
				if($(document).height() <= totalheight){
					if(stop==true){
						stop=false;
						$('.favorite_main').append('<div id="favorite_loading"><i class="fa fa-spinner fa-spin"></i> 正在加载...</div>');
						page++;
						core.json('shop/favorite', {page:page}, function(morejson) {
							stop = true;
							$('#favorite_loading').remove();
							$(".favorite_main").append(tpl('favorite_list', morejson.result));
							bindEvents();
							if (morejson.result.list.length <morejson.result.pagesize) {
								$('.favorite_main').append('<div id="favorite_loading">已经加载完全部收藏</div>');
								loaded = true;
								$(window).scroll = null;
								return;
							}
						},true);
					}
				}
			});
		},true);
	});
</script>
{php $show_footer = true}
{template 'common/footer'}